﻿using ZhouYu.DreamTour.Core;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhouYu.DreamTour.DAL.Repository
{
    public class SmsInfoRepository:ISmsInfoRepository
    {
        /// <summary>
        /// 添加短信发送记录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public int AddSmsInfo(SmsInfo smsInfo)
        {
            int result = -1;
            //sql语句
            string sql = "insert into SmsInfo(Code,TelPhone,CreateTime,ExpireTime) values(@Code,@TelPhone,@CreateTime,@ExpireTime)";
            smsInfo.CreateTime = DateTime.Now;
            smsInfo.ExpireTime = DateTime.Now.AddMinutes(5);
            //调用添加方法
            return DapperHelper<SmsInfo>.Execute(sql, smsInfo);
        }



        /// <summary>
        /// 查询验证码是否存在
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public int CheckCode(SmsInfo smsInfo)
        {
            int result = -1;
            //sql语句
            string sql = "select count(1) from SmsInfo where Code=@Code and TelPhone=@TelPhone and ExpireTime>@ExpireTime";
            smsInfo.ExpireTime = DateTime.Now;
            //调用查询方法
            return Convert.ToInt32(DapperHelper<SmsInfo>.ExecuteScalar(sql, smsInfo));
        }



        /// <summary>
        /// 根据手机号查询用户是否存在
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public UserInfo IFExistUserByPhone(long phone)
        {
            int result = -1;
            //sql语句
            string sql = "select * from UserInfo where User_Telphone=@User_Telphone";
            //创建UserInfo对象
            UserInfo userInfo = new UserInfo();
            userInfo.User_Telphone = phone;
            //调用查询方法            
            return DapperHelper<UserInfo>.QuerySingleOrDefault(sql, userInfo);
        }


        /// <summary>
        /// 如果手机号未注册便将手机号存入数据库
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public int AddPhoneIntoUser(long phone)
        {
            int result = -1;
            //sql语句
            string sql = "insert into UserInfo(User_Name,User_RealName,User_Pwd,User_Sex,User_Telphone,User_CreateTime,User_UpdateTime)values(@User_Name,default,default,default,@User_Telphone,@User_CreateTime,@User_UpdateTime)";
            //创建UserInfo对象
            UserInfo userInfo = new UserInfo();
            //赋值
            userInfo.User_Name = phone.ToString();
            userInfo.User_Telphone = phone;
            userInfo.User_CreateTime = DateTime.Now;
            userInfo.User_UpdateTime = DateTime.Now;
            //调用添加方法
            return DapperHelper<UserInfo>.Execute(sql, userInfo);
        }
    }
}
